package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.User;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.UserService;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user/*")
public class UserServlet extends BaseServlet {
    private UserService userService = BeanFactory.getBean("userService", UserService.class);

    private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {

            List<User> list = userService.findAll();
            objectMapper.writeValue(response.getWriter(), new Result(true, MessageConstant.QUERY_DEPT_SUCCESS, list));
        } catch (IOException e) {
            e.printStackTrace();
            objectMapper.writeValue(response.getWriter(), new Result(false, MessageConstant.QUERY_DEPT_FAIL));
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);

            userService.delete(ids);
            objectMapper.writeValue(response.getWriter(), new Result(true, MessageConstant.DELETE_COMPANY_SUCCESS));
        } catch (IOException e) {
            e.printStackTrace();
            objectMapper.writeValue(response.getWriter(), new Result(false, MessageConstant.DELETE_COMPANY_FAIL));
        }
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            User user = BeanUtil.fillBeanFromJson(request, User.class);

            userService.update(user);
            objectMapper.writeValue(response.getWriter(), new Result(true, MessageConstant.EDIT_COMPANY_SUCCESS));
        } catch (IOException e) {
            e.printStackTrace();
            objectMapper.writeValue(response.getWriter(), new Result(false, MessageConstant.EDIT_COMPANY_FAIL));
        }
    }

    private void findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String id = request.getParameter("id");

            User user = userService.findById(id);
            objectMapper.writeValue(response.getWriter(), new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS, user));
        } catch (IOException e) {
            e.printStackTrace();
            objectMapper.writeValue(response.getWriter(), new Result(false, MessageConstant.QUERY_COMPANY_FAIL));
        }
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            User user = BeanUtil.fillBeanFromJson(request, User.class);

            userService.save(user);
            objectMapper.writeValue(response.getWriter(), new Result(true, MessageConstant.SAVE_COMPANY_SUCCESS));
        } catch (IOException e) {
            e.printStackTrace();
            objectMapper.writeValue(response.getWriter(), new Result(false, MessageConstant.SAVE_COMPANY_FAIL));
        }
    }

    private void findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            int currentPage = 1, pageSize = 5;
            if (!StringUtils.isBlank(currentPageStr)) {
                currentPage = Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)) {
                pageSize = Integer.parseInt(pageSizeStr);
            }

            PageInfo<User> info = userService.findByPage(currentPage, pageSize);
            objectMapper.writeValue(response.getWriter(), new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS, info));
        } catch (IOException e) {
            e.printStackTrace();
            objectMapper.writeValue(response.getWriter(), new Result(false, MessageConstant.QUERY_COMPANY_FAIL));
        }

    }


}
